"""
将目前生成的 geojson 转换成 histomics 格式的 annotations json

原始 geojson 格式：
{

    "type": "FeatureCollection",
    "features": [
        {
            "type": "feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            35550.0,
                            50714.0
                        ],
                        [
                            35550.0,
                            50774.0
                        ],
                        [
                            35610.0,
                            50774.0
                        ],
                        [
                            35610.0,
                            50714.0
                        ],
                        [
                            35550.0,
                            50714.0
                        ]
                    ]
                ]
            },
            "properties": {
                "object_type": "annotation",
                "classification": {
                    "colorRGB": -16711681
                },
                "isLocked": false
            }
        }
    ]
}
target json
{
    "annotation": {
        "elements": [
            {
                "type": "rectangle",
                "fillColor": "rgba(0,0,0,0)",
                "lineColor": "rgb(0,255,0)",
                "rotation": 0,
                "center": [
                    39030,
                    3868,
                    0
                ],
                "width": 40,
                "height": 40
            }
        ]
    }
}

"""
import copy
import os
import json

annotation = '{"annotation":{"elements":[],"name":""}}'
his_obj = json.loads(annotation)
element_json = '{"type":"rectangle","fillColor":"rgba(0,0,0,0)","lineColor":"rgb(0,255,0)","rotation":0,"center":[],"width":0,"height":0}'
element_obj = json.loads(element_json)

folder_path = "/home/hsmy/桌面/微浸润结果case/json"
target_path = "/home/hsmy/桌面/微浸润结果case/json_his/"
if not os.path.exists(target_path):
    os.makedirs(target_path)
# 遍历指定文件夹下的所有文件
for root, dirs, files in os.walk(folder_path):
    for file in sorted(files):
        try:
            # 打开并解析 JSON 文件
            file_path = os.path.join(root, file)
            with open(file_path, 'r') as f:
                data = json.load(f)
                features_ = data['features']
                elements = []
                for feature in features_:
                    '''
                    (p1) --- (p4)
                    |           |
                    (p2) --- (p3)
                    '''
                    point1 = feature['geometry']['coordinates'][0][0]
                    point2 = feature['geometry']['coordinates'][0][1]
                    point3 = feature['geometry']['coordinates'][0][2]
                    width = point3[0] - point2[0]
                    height = point2[1] - point1[1]

                    point = [0, 0, 0]  # center
                    point[0] = point1[0] + width // 2
                    point[1] = point1[1] + height // 2
                    element = copy.deepcopy(element_obj)
                    element['center'] = point
                    element['width'] = width
                    element['height'] = height
                    elements.append(element)
                v = copy.deepcopy(his_obj)
                v['annotation']['elements'] = elements
                v['annotation']['name'] = file.split('.')[0] + '_geo'
                file_name = target_path + file_path.split('/')[-1]
                with open(file_name, "w") as file_:
                    json.dump(v, file_)
        except json.JSONDecodeError as e:
            print(f"Error decoding JSON file: {e}")
